export const phone = (required = true, trigger = 'blur') => {
  return [{
    required: required,
    message: '请输入手机号码',
    trigger: trigger
  }, { validator: function (rule, value, callback) {
    if (value && !/^1[3456789]\d{9}$/.test(value)) {
      callback(new Error('请输入正确的手机号'))
    } else {
      callback()
    }
  },
  trigger: 'blur'
  }]
}

export const email = (required = true, trigger = 'blur') => {
  return [
    { required: required, message: '请输入邮箱地址', trigger: trigger },
    {
      type: 'email',
      message: '请输入正确的邮箱地址',
      trigger: ['blur', 'change']
    }
  ]
}

export const username = (required = true, trigger = 'blur') => {
  return [
    {
      required: required,
      message: '请输入用户名',
      trigger: trigger
    },
    // {
    //   min: 2,
    //   max: 16,
    //   message: '长度在 2 到 16 个字符'
    // },
    {
      pattern: /^[_0-9a-zA-Z\u4E00-\u9FA5]{2,16}$/,
      message: '只能输入2-16个字母、数字或中文'
    }
  ]
}

// 姓名
export const realName = (required = true, trigger = 'blur') => {
  return [
    {
      required: required,
      message: '请输入姓名',
      trigger: trigger
    }, {
      min: 2,
      max: 16,
      message: '长度在 2 到 16 个字符'
    }, {
      pattern: /^[\u4E00-\u9FA5]+$/,
      message: '姓名只能为中文'
    }
  ]
}

export const password = () => {
  return [{
    min: 6,
    max: 16,
    message: '长度在 6 到 16 个字符'
  }, {
    pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/,
    message: '密码为6-16位数字字母组合'
  }]
}

export const verificationCode = (required = true, trigger = 'blur') => {
  return [{
    required: required,
    message: '验证码',
    trigger: trigger
  }, {
    min: 6,
    max: 6,
    message: '请输入6位验证码'
  }, {
    pattern: /^[0-9]{6}$/,
    message: '只能输入数字'
  }]
}

export const checkIdCard = function (num) {
  num = num.toUpperCase()
  // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X。
  if (!(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(num))) {
    // alert('输入的身份证号长度不对，或者号码不符合规定！\n15位号码应全为数字，18位号码末位可以为数字或X。');
    return false
  }
  // 校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
  // 下面分别分析出生日期和校验位
  let len, re
  len = num.length
  if (len === 15) {
    re = new RegExp(/^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/)
    let arrSplit = num.match(re)
    // 检查生日日期是否正确
    let dtmBirth = new Date('19' + arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4])
    let bGoodDay = (dtmBirth.getYear() === Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) === Number(arrSplit[3])) && (dtmBirth.getDate() === Number(arrSplit[4]))
    if (!bGoodDay) {
      // alert('输入的身份证号里出生日期不对！');
      return false
    } else {
      // 将15位身份证转成18位
      // 校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
      let arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
      let arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
      let nTemp = 0
      num = num.substr(0, 6) + '19' + num.substr(6, num.length - 6)
      for (let i = 0; i < 17; i++) {
        nTemp += num.substr(i, 1) * arrInt[i]
      }
      num += arrCh[nTemp % 11]
      return num
    }
  }
  if (len === 18) {
    re = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/)
    let arrSplit = num.match(re)
    // 检查生日日期是否正确
    let dtmBirth = new Date(arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4])
    let bGoodDay
    bGoodDay = (dtmBirth.getFullYear() === Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) === Number(arrSplit[3])) && (dtmBirth.getDate() === Number(arrSplit[4]))
    if (!bGoodDay) {
      // alert(dtmBirth.getYear());
      // alert(arrSplit[2]);
      // alert('输入的身份证号里出生日期不对！');
      return false
    } else {
      // 检验18位身份证的校验码是否正确。
      // 校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
      let valnum
      let arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
      let arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
      let nTemp = 0
      for (let i = 0; i < 17; i++) {
        nTemp += num.substr(i, 1) * arrInt[i]
      }
      valnum = arrCh[nTemp % 11]
      if (valnum !== num.substr(17, 1)) {
        // alert('18位身份证的校验码不正确！应该为：' + valnum);
        return false
      }
      return num
    }
  }
  if (len > 18) {
    return false
  }
  return false
}
// 验证https、http
export const url = (required = true, trigger = 'blur') => {
  return [
    {
      required: required,
      message: '请输入url',
      trigger: trigger
    },
    {
      pattern: /^(?:http(s)?:\/\/)[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=]+$/,
      message: '请输入正确的url',
      trigger: trigger
    },
    {
      max: 500,
      message: '输入的url长度不能超过500个字符',
      trigger: trigger
    }
  ]
}
